var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// CORS
/*app.use((req, res, next) => {
  if(req.path !== '/' && !req.path.includes('.')){
    /!*res.header({
      'Access-Control-Allow-Credentials': true,
      'Access-Control-Allow-Origin': req.headers.origin || '*',
      'Access-Control-Allow-Headers': 'X-Requested-With',
      'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS',
      'Content-Type': 'application/json; charset=utf-8'
    })*!/
  }
  next()
})*/
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
// ********** 为需要登录
app.get('/api/search',require('./routes/spider'));//搜索歌曲
app.get('/api/personalized',require('./routes/personalized'));// 推荐歌单
app.get('/api/personalized/newsong',require('./routes/personalized_newSong')); // 最新音乐
app.get('/api/search/hot',require('./routes/search_hot')); // 热搜
app.get('/api/top/list',require('./routes/top_list')); // 排行榜
app.get('/api/song/detail',require('./routes/song_detail')); // 歌曲详情
app.get('/api/song/like',require('./routes/likeSong')); // 喜欢歌曲  **********
app.get('/api/lyric',require('./routes/lyric'));  // 歌曲歌词
app.get('/api/simi/playlist',require('./routes/simi_playlists')); // 包含这首歌的歌单
app.get('/api/song/comment',require('./routes/comment_music')); // 歌曲评论
app.get('/api/playlist/detail',require('./routes/playList_detail')); // 歌单详情
app.get('/api/login',require('./routes/login')); // 登录
app.get('/api/login/status',require('./routes/login_status')); // 获取登录状态
app.get('/api/comment/like',require('./routes/comment_like')); // 给评论点赞
app.get('/api/likeList',require('./routes/likeList')); // 喜欢的歌曲id列表
app.get('/api/loginOut',require('./routes/loginOut')); // 退出登录
app.get('/api/song/url',require('./routes/song_url')); // 获取播放链接
app.get('/api/user/cloud',require('./routes/user_cloud')); // 获取云盘歌曲
app.get('/api/playlist/subscribe',require('./routes/playList_subscribe')); // 收藏歌单
app.get('/api/user/playlist',require('./routes/user_playlist')); // 获取用户歌单
app.get('/api/user/detail',require('./routes/user_detail')); // 获取用户信息
/*--------------new----------------------*/
app.get('/api/playList/comment',require('./routes/new/comment_playList')); // 获取歌单评论
app.get('/api/search/suggest',require('./routes/new/search_suggest')); // 获取搜索建议
app.get('/api/banner',require('./routes/new/banner')); // 获取首页轮播图
app.get('/api/top/mv',require('./routes/mv/top_mv')); // 获取mv排行榜
app.get('/api/mv/detail',require('./routes/mv/mv_detail')); // 获取mv详情

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});
app.listen(80, function(){
  console.log('http://localhost:80')
  console.log('服务器已运行.....')
})
module.exports = app;
